package com.leocops.moviezona.util;

import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;


public class WebUtil {
	
	private static Logger logger = Logger.getLogger(WebUtil.class);
	
	/**
	 * Function that return the current HttpRequest
	 * @return HttpServletRequest
	 */
	public static HttpServletRequest getRequest() {
		
		HttpServletRequest request  = null;
		request = (HttpServletRequest)FacesContext.getCurrentInstance().
									getExternalContext().getRequest();
		return request;
	}
	
	/**
	 * Function that return the JSF Managed bean by name
	 * @return Object
	 */
	public static Object getJSFBean(String beanName) {
		Object bean = null;
		FacesContext facesContext = FacesContext.getCurrentInstance();
		bean = facesContext.getApplication()
	     	.createValueBinding("#{"+ beanName + "}").getValue(facesContext);
		
		return bean;
	}
	
	/**
	 * Function that return the value from property file
	 * by key
	 * @return String
	 */
	public String getPropertyValue(String key) {
		
		String value ="";
		   Properties properties=new Properties();  
		   try {
			   properties.load(Thread.currentThread().getContextClassLoader().getResource("/moviezona.properties").openStream());
			   value = properties.getProperty(key);
		} catch (IOException e) {
			logger.debug(e.getMessage());
			e.printStackTrace();
		}  
		return value;
	}
	
	/**
	 * Function that return ResourceBundle
	 * by bundle name
	 * @return ResourceBundle
	 */
	public ResourceBundle getResourceBundle(String bundleName) {
		
		ResourceBundle bundle = null;
		FacesContext facesContext = FacesContext.getCurrentInstance();
		bundle = ResourceBundle.getBundle(bundleName, 
								facesContext.getViewRoot().getLocale());
		return bundle;
	}

}
